﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace WJ.Infrastructure.Extension
{
    public static class ListExtension
    {
        public static List<T> TryToList<T>(this ICollection<T> icol)
        {
            if (icol != null)
            {
                return icol.ToList<T>();
            }
            else
            {
                return new List<T>();
            }
        }

        public static T SingOrNew<T>(this IEnumerable<T> list, Expression<Func<T, bool>> exp) where T : class, new()
        {
            if (list == null)
                return new T();

            T t = list.AsQueryable().SingleOrDefault(exp);

            if (t == null)
            {
                t = new T();
            }

            return t;
        }
    }
}
